<!--
  * BOM表
  *
  * @Author:    AKITA
  * @Date:      2025-08-15 10:54:43
  * @Copyright  AKITA
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="主键" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.docEntry" placeholder="主键" />
      </a-form-item>
      <a-form-item label="用户名称" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.userName" placeholder="用户名称" />
      </a-form-item>

      <a-form-item label="月期间" class="smart-query-form-item">
        <a-date-picker format="YYYY-MM" valueFormat="YYYY-MM" v-model:value="queryForm.absId" picker="month" placeholder="月期间" />
      </a-form-item>

      <a-form-item label="认证日期从" class="smart-query-form-item">
        <a-date-picker
          valueFormat="YYYY-MM-DD HH:mm:ss"
          showTime
          style="width: 200px"
          v-model:value="queryForm.authDateTimeFr"
          placeholder="认证日期从"
        />
      </a-form-item>
      <a-form-item label="认证日期到" class="smart-query-form-item">
        <a-date-picker
          valueFormat="YYYY-MM-DD HH:mm:ss"
          showTime
          style="width: 200px"
          v-model:value="queryForm.authDateTimeTo"
          placeholder="认证日期到"
        />
      </a-form-item>

      <a-form-item class="smart-query-form-item">
        <a-button type="primary" @click="onSearch">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery" class="smart-margin-left10">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div>
        <a-button-group>
          <a-button @click="showForm" type="primary">
            <template #icon>
              <PlusOutlined />
            </template>
            新建
          </a-button>
          <a-button @click="onExport" type="primary">
            <template #icon>
              <SelectOutlined />
            </template>
            导出
          </a-button>
        </a-button-group>
      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="null" :refresh="queryData" />
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格 begin ----------->
    <a-table
      rowKey="docEntry"
      :dataSource="tableData"
      :columns="columns"
      :loading="tableLoading"
      :pagination="false"
      :scroll="{ x: 600, y: 900 }"
      style="width: 100%"

    >
      <template #bodyCell="{ text, record, column, index }">
        <template v-if="column.dataIndex === 'direction'">
          <a-tag v-if="text === '0'" color="#ff0000">下班</a-tag>
          <a-tag v-if="text === '1'" color="#00ff00">上班</a-tag>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button size="small" @click="onDelete(record)" type="link" danger>删除</a-button>
          </div>
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageNum"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>

    <TmsordForm ref="formRef" @reloadList="queryData" />
  </a-card>
</template>
<script setup>
  import { reactive, ref, onMounted } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import dayjs from 'dayjs';
  import { tmsordApi } from '/@/api/business/erp/tmsord-api';
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import { smartSentry } from '/@/lib/smart-sentry';
  import TableOperator from '/@/components/support/table-operator/index.vue';
  import TmsordForm from './components/tmsord-form.vue';
  import Login from '/@/views/system/login3/login.vue';

  // ---------------------------- 表格列 ----------------------------

  const columns = ref([
    {
      title: '主键',
      dataIndex: 'docEntry',
      width: 100,
    },
    {
      title: '用户编码',
      dataIndex: 'userId',
      width: 100,
    },
    {
      title: '用户名称',
      dataIndex: 'userName',
      width: 200,
    },
    {
      title: '认证日期时间',
      dataIndex: 'authDateTime',
      width: 300,
    },
    {
      title: '方向',
      dataIndex: 'direction',
      width: 100,
    },
    {
      title: '设备编码',
      dataIndex: 'macId',
      ellipsis: true,
      width: 300,
    },
    {
      title: '设备名称',
      dataIndex: 'macName',
      width: 200,
    },
    {
      title: '卡号',
      dataIndex: 'cardId',
      width: 100,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      align: 'center',
      width: 160,
    },
  ]);

  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    docEntry: undefined, //主键
    pageNum: 1,
    pageSize: 30,
    authDateTimeFr: null,
    authDateTimeTo: null,
    absId: null,
  };

  // 查询表单form
  const queryForm = reactive({ ...queryFormState });
  // 表格加载loading
  const tableLoading = ref(false);
  // 表格数据
  const tableData = ref([]);
  // 总数
  const total = ref(0);

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize;
    Object.assign(queryForm, queryFormState);
    queryForm.pageSize = pageSize;
    queryData();
  }

  // 搜索
  function onSearch() {
    queryForm.pageNum = 1;
    queryData();
  }

  // 查询数据
  function queryData() {
    tableLoading.value = true;

    tmsordApi
      .queryPage(queryForm)
      .then((res) => {
        tableData.value = res.data.list;
        total.value = res.data.total;
      })
      .catch((e) => {
        smartSentry.captureError(e);
      })
      .finally(() => {
        tableLoading.value = false;
      });
  }

  onMounted(queryData);

  // ---------------------------- 添加/修改 ----------------------------
  const formRef = ref();

  function showForm(data) {
    formRef.value.show(data);
  }

  // ---------------------------- 单个删除 ----------------------------
  //确认删除
  function onDelete(data) {
    Modal.confirm({
      title: '提示',
      content: '确定要删除选吗?',
      okText: '删除',
      okType: 'danger',
      onOk() {
        requestDelete(data);
      },
      cancelText: '取消',
      onCancel() {},
    });
  }

  //请求删除
  function requestDelete(data) {
    tableLoading.value = true;
    tmsordApi
      .delete(data.docEntry)
      .then((res) => {})
      .catch((err) => {
        smartSentry.captureError(err);
      })
      .finally(() => {
        tableLoading.value = false;
        message.success('删除成功');
        queryData();
      });
  }

  function setRowClassName(record, index) {
    const hours = new Date(record.authDateTime).getHours();
    return ((hours < 8 ) || (hours > 18 )) ? '' : 'wainBackground';
  }

  function onExport() {
    tmsordApi.export(queryForm);
  }
</script>

<style  >
  .wainBackground {
    background-color: #cccccc;
  }
</style>
